package ink.xiaobaibai.common;

import ink.xiaobaibai.units.ImageCodeFactory;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.RandomStringUtils;

import java.awt.image.BufferedImage;

/**
 * @description: 图片验证码
 * @author: 小白白
 * @create: 2021-05-13
 **/

@Data
@EqualsAndHashCode(callSuper = false)
public class ImageVerificationCode extends GeneralVerificationCode {

    /**
     * 设置图片宽度
     */
    private static int w = 100;
    /**
     * 设置图片高度
     */
    private static int h = 40;

    /**
     * 图片主体
     */
    private BufferedImage image;

    public ImageVerificationCode() {
        ImageCodeFactory imageCodeFactory = new ImageCodeFactory(this.code, ImageVerificationCode.w, ImageVerificationCode.h);
        this.image = imageCodeFactory.getImage();
    }

    public ImageVerificationCode(int w, int h) {
        //自定义长宽
        ImageCodeFactory imageCodeFactory = new ImageCodeFactory(this.code, w, h);
        this.image = imageCodeFactory.getImage();
    }

    public BufferedImage getImage() {
        return this.image;
    }

    @Override
    protected String getRandomCode() {
        return RandomStringUtils.randomAlphanumeric(codeLength);
    }

}
